package com.py.product.service.impl;

import java.util.List;

import com.py.product.domain.WhInventory;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.py.product.domain.PySuppliers;
import com.py.product.mapper.ProductMapper;
import com.py.product.domain.Product;
import com.py.product.service.IProductService;

import javax.annotation.Resource;

/**
 * 货品信息Service业务层处理
 * 
 * @author he
 * @date 2024-11-22
 */
@Service
public class ProductServiceImpl implements IProductService 
{
    @Resource
    private ProductMapper productMapper;

    @Override
    public Product selectProductDetails(Long productId) {
        return productMapper.selectProductDetails(productId);
    }

    @Override
    public List<PySuppliers> selectPySuppliers() {
        return productMapper.selectPySuppliers();
    }

    /**
     * 查询货品信息
     * 
     * @param productId 货品信息主键
     * @return 货品信息
     */
    @Override
    public Product selectProductByProductId(Long productId)
    {
        return productMapper.selectProductByProductId(productId);
    }

    /**
     * 查询货品信息列表
     * 
     * @param product 货品信息
     * @return 货品信息
     */
    @Override
    public List<Product> selectProductList(Product product)
    {
        return productMapper.selectProductList(product);
    }

    @Override
    public List<Product> addProductByname(int warehouseId) {
        return productMapper.addProductByProductName(warehouseId);
    }

    @Override
    public List<Product> addProductByname1(int warehouseId) {
        return productMapper.addProductByProductName(warehouseId);
    }


    /**
     * 新增货品信息
     * 
     * @param product 货品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProduct(Product product)
    {
        product.setCreateTime(DateUtils.getNowDate());
        int rows = productMapper.insertProduct(product);
        insertPySuppliers(product);
        return rows;
    }

    @Override
    public int xgai(WhInventory whInventory) {
        return productMapper.xgai(whInventory);
    }

    /**
     * 修改货品信息
     * 
     * @param product 货品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProduct(Product product)
    {
        product.setUpdateTime(DateUtils.getNowDate());
        productMapper.deletePySuppliersBySupplierId(product.getProductId());
        insertPySuppliers(product);
        return productMapper.updateProduct(product);
    }

    /**
     * 批量删除货品信息
     * 
     * @param productIds 需要删除的货品信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductByProductIds(Long[] productIds)
    {
        productMapper.deletePySuppliersBySupplierIds(productIds);
        return productMapper.deleteProductByProductIds(productIds);
    }

    /**
     * 删除货品信息信息
     * 
     * @param productId 货品信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductByProductId(Long productId)
    {
        productMapper.deletePySuppliersBySupplierId(productId);
        return productMapper.deleteProductByProductId(productId);
    }

    /**
     * 新增供应商信息信息
     * 
     * @param product 货品信息对象
     */
    public void insertPySuppliers(Product product)
    {
        List<PySuppliers> pySuppliersList = product.getPySuppliersList();
        Long productId = product.getProductId();
        if (StringUtils.isNotNull(pySuppliersList))
        {
            List<PySuppliers> list = new ArrayList<PySuppliers>();
            for (PySuppliers pySuppliers : pySuppliersList)
            {
                pySuppliers.setSupplierId(productId);
                list.add(pySuppliers);
            }
            if (list.size() > 0)
            {
                productMapper.batchPySuppliers(list);
            }
        }
    }

    @Override
    public List<Product> selectAllByProduct(Product product) {
        return productMapper.selectAllByProduct(product);
    }
}
